package mspdt.ubzuuch.mn;

import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

public class ItemActivityFromDB extends Activity{
	
	private boolean isFromDB = true;
	private int id=0;
	private DBAdapter db;
	private ArrayList<String> titles = new ArrayList<String>();
	private ArrayList<String> bodys = new ArrayList<String>();
	private ArrayList<String> prices = new ArrayList<String>();
	private ArrayList<String> images = new ArrayList<String>();
	private ArrayList<Integer> ids = new ArrayList<Integer>();
	private ArrayList<String> categories = new ArrayList<String>();
	private ListAdapterOfDB list_adapter;
	private ListView listview;
	@Override
	public void onCreate(Bundle bundle)
	{
		super.onCreate(bundle);
		setContentView(R.layout.item_body_db);
		listview=(ListView)findViewById(R.id.item_list_db);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ItemActivityFromDB.this, ViewActivity.class);
				Bundle bundle = new Bundle();
				bundle.putInt("title", 0);
				bundle.putString("id", String.valueOf(ids.get(arg2)));
				bundle.putBoolean("isfromdb", isFromDB);
				intent.putExtras(bundle);
				startActivity(intent);
				overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
			}
		});
		listview.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				final int pos = arg2;
				AlertDialog.Builder dialog = new AlertDialog.Builder(ItemActivityFromDB.this);
				dialog.setTitle("Устгах");
				dialog.setMessage("Та энэ мэдээг хадгалагдсан мэдээлэл дундаас устгах гэж байна. Та устгахдаа итгэлтэй байна уу?");
				dialog.setPositiveButton("Тийм", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
						
						final ProgressDialog dialog_wait = ProgressDialog.show(ItemActivityFromDB.this, null, 
				    			getResources().getString(R.string.dialog_please_wait), true);
				    	
				    			final Handler handler = new Handler() {
				    			   public void handleMessage(Message msg) {
				    				   //RUN доторх үйлдэл хийгдэж дуусахад уншсан мэдээллүүдээ adapter-т хийж өгнө
//				    					listview.post(new Runnable() {
//			    				        public void run() {
//			    				        	list_adapter.notifyDataSetChanged();
//			    				        }
//				    					});
				    				   list_adapter=new ListAdapterOfDB(ItemActivityFromDB.this, titles, bodys, prices, images, categories, ids);
				    				   listview.setAdapter(list_adapter);
				    				   dialog_wait.dismiss();
				    			      }
				    			   };
				    			Thread checkUpdate = new Thread() {  
				    			   public void run() {
				    				   //энэ дотор Json-с дата уншиж харгалзах утгуудад нь хийж өгнө
				    				   db.open();
				    				   db.deleteData(ids.get(pos));
				    					titles = db.getTitles();
				    					bodys=db.getBodys();
				    					prices=db.getPrices();
				    					images=db.getListImages();
				    					ids=db.getIds();
				    					categories=db.getCategories();
				    					db.close();
				    				   handler.sendEmptyMessage(0);
				    			      }
				    			   };
				    			checkUpdate.start();
						
					}
				});
				dialog.setNegativeButton("Үгүй", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				});
				dialog.show();
				return false;
			}
		});
		db = new DBAdapter(this);
		createDataFromDB();
		createTitleComponents();
	}
	private void createDataFromDB()
	{
		
		
		final ProgressDialog dialog = ProgressDialog.show(ItemActivityFromDB.this, null, 
    			getResources().getString(R.string.dialog_please_wait), true);
    	
    			final Handler handler = new Handler() {
    			   public void handleMessage(Message msg) {
    				   //RUN доторх үйлдэл хийгдэж дуусахад уншсан мэдээллүүдээ adapter-т хийж өгнө
    				   list_adapter=new ListAdapterOfDB(ItemActivityFromDB.this, titles, bodys, prices, images, categories, ids);
    				   listview.setAdapter(list_adapter);
    				   dialog.dismiss();
    			      }
    			   };
    			Thread checkUpdate = new Thread() {  
    			   public void run() {
    				   //энэ дотор Json-с дата уншиж харгалзах утгуудад нь хийж өгнө
    				   db.open();
    					titles = db.getTitles();
    					bodys=db.getBodys();
    					prices=db.getPrices();
    					images=db.getListImages();
    					ids=db.getIds();
    					categories=db.getCategories();
    					db.close();
    				   handler.sendEmptyMessage(0);
    			      }
    			   };
    			checkUpdate.start();
	}
  	@Override
	public void onBackPressed() {
	   finish();
	   overridePendingTransition(R.anim.left_in, R.anim.top_out);
	}
	public void createTitleComponents()
	{
		//Хуудасны дээр байрлах толгой хэсгийн хийх үйлдлүүд
		
		//Үндсэн товчлууруудын үйлдэл
		//Буцах товчлуур
		findViewById(R.id.back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
				overridePendingTransition(R.anim.left_in, R.anim.top_out);
			}
		});
		//Хадгалсан мэдээ унших товчлуур
		findViewById(R.id.title_history).setVisibility(View.GONE);
		//Хайх товчлуур
		findViewById(R.id.title_search).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(ItemActivityFromDB.this, SearchActivity.class));
				overridePendingTransition(R.anim.top_in, R.anim.top_out);
			}
		});
		//Тухай товчлуур
		findViewById(R.id.title_info).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(ItemActivityFromDB.this, InformationActivity.class));
				overridePendingTransition(R.anim.top_in, R.anim.no_anim);
			}
		});
		//Home буюу лого дээр дарах үйлдэл
		findViewById(R.id.home).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			}
		});
		
		
		
		//Програмын үндсэн менюны үйлдлүүд
		findViewById(R.id.title_menu_1).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ItemActivityFromDB.this, MainActivity.class); 
				intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); 
				startActivity(intent);
				finish();
				overridePendingTransition(R.anim.left_in, R.anim.top_out);
			}
		});
		findViewById(R.id.title_menu_2).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ItemActivityFromDB.this, RentActivity.class); 
				intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); 
				startActivity(intent);
				finish();
				overridePendingTransition(R.anim.left_in, R.anim.top_out);
			}
		});
		findViewById(R.id.title_menu_3).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ItemActivityFromDB.this, NewApartmentActivity.class); 
				intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); 
				startActivity(intent);
				finish();
				overridePendingTransition(R.anim.left_in, R.anim.top_out);
			}
		});
		findViewById(R.id.title_menu_4).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ItemActivityFromDB.this, UBActivity.class); 
				intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); 
				startActivity(intent);
				finish();
				overridePendingTransition(R.anim.left_in, R.anim.top_out);
			}
		});
		findViewById(R.id.title_menu_5).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ItemActivityFromDB.this, AutomobileActivity.class); 
				intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); 
				startActivity(intent);
				finish();
				overridePendingTransition(R.anim.left_in, R.anim.top_out);
			}
		});
	}
	
}
